<?php
Cinnamon::loadClass('Container');

/**
 * Default view class
 */
class View extends Container {
	/**
	 * URL to view directory
	 */
	protected $url;
	/**
	 * PATH to view directory
	 */
	protected $path;
	/**
	 * Registered variables
	 */
	protected $variables;
	/**
	 * Registered messages
	 */
	protected $messages;
	
	// Type of messages
	const NOTICE 	= 'notice';	
	const HELP 		= 'help';
	const WARNING 	= 'warning';
	const ERROR 	= 'error';
	
	/**
	 * Default constructor
	 */
	public function __construct() {
		parent::__construct();
		
		$this->url = CSEURL .'/view/';
		$this->path = CSEPATH .'/view/';
		$this->variables = array();
		$this->messages = array();
	}
	
	/**
	 * Returns the full file path of the specified file path. 
	 *
	 * @param string path the file path
	 * @return the full file path
	 */
	protected function getFilePath($path) {
		if ( file_exists($path) ) {
			return $path;
		} else if ( $this->path . $path ) {
			return ( $this->path . $path );
		} else if ( CSEPATH .'/'. $path ) {
			return ( CSEPATH .'/'. $path );
		} else {
			throw new Exception('File <code>'. $path .'</code> does not exist');
		}
	}

	
	/**
	 * Renders the specified file. Any registered variables will be extracted, so can be used in that file. The specified file may be an absolute path or a  relative path to the Cinnamon root (that is CSEURL) or to the view directory.
	 *
	 * @param string file the file to be rendered
	 */
	public function render() {
		if ( func_num_args() == 1 ) {
			$file = func_get_arg(0);
			$file = $this->getFilePath($file);
			extract($this->variables);	
			include $file;
		} else {
			$this->renderHeader();
			foreach ( $this->getAllChildren() as $child )
				$child->render();
			$this->renderFooter();
		}
	}
	
	public function renderHeader() {
		if ( !Cinnamon::inMode('ajax') ) {
			$this->render('header.php');
		}
	}
	
	public function renderFilters() {
		if ( !Cinnamon::inMode('ajax') ) {
			$this->render('filters.php');
		}
	}
	
	public function renderFooter() {
		Cinnamon::registry('events')->dispatchEvent( new Event('content', 'page', CSEFILE) );
		if ( !Cinnamon::inMode('ajax') ) {		
			$this->render('footer.php');
		}
	}
	
	//
	// Variable functions
	//
	public function setVariable($name, $value) {
		$this->variables[$name] = $value;
	}
	
	//
	// URL and Path functions
	//
	public function getUrl($file=null) {
		return ( empty($file) ) ? $this->url : $this->url . $file;
	}
	
	//
	// Messages functions
	//
	public function addMessage($type, $message) {
		if ( !is_array($this->messages[$type]) ) {
			$this->messages[$type] = array();
		}
		array_push($this->messages[$type], $message);
	}
	
	public function showMessages($type=null) {
		if ( $type == null ) {
			foreach ( $this->messages as $type=>$messages ) {
				foreach ( $messages as $message ) {
					echo '<div class="'. $type .'">'. $message .'</div>';
				}
			}
		} else {
			echo '<div class="'. $type .'">';
			foreach ( $messages as $message ) {
				echo $message .'<br />';
			}
			echo '</div>';		
		}
	}
	
	public function clearMessages() {
		$this->messages = array();
	}
	
}
?>